package com.wuchao.controller;

import com.wuchao.response.R;
import com.wuchao.service.AliOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @Author wuchao66
 * @Date 2023/12/20 10:54
 * @PackageName:com.wuchao.controller
 * @ClassName: AiossController
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
@RestController
@Api("阿里云")
@CrossOrigin
public class AiossController {
    @Resource
    AliOssService aliOssService;
    //上传头像
    @ApiOperation(value = "上传图片文件")
    @PostMapping("/uploadImgFile")
    public R uploadImgFile(MultipartFile file){
        String s = aliOssService.upload(file);
        return R.ok().data("url",s);
    }
    @ApiOperation(value = "删除上传替换之后的头像")
    @PostMapping("/deleteImgFile")
    public R deleteImgFile(String file){
        //https://xinguan-parent.oss-cn-hangzhou.aliyuncs.com/2020/09/25/1575345b2cd14c13872f9b83a0aac919.png
        try {
            String[] splitFile = file.split(".com/");
            aliOssService.deleteFile(splitFile[1]);
            return R.ok();
        }catch (Exception e){
            return R.error();
        }
    }

}
